/*
	jGrouse framework
	Copyright 2007 by Denis Riabtchik 
	All rights reserved.
	
	Licensed under modified BSD license. See license.txt for details
*/


/**
	@module jgrouse.widgets.Expander
	@requires jgrouse.widgets.Container
*/

jgrouse.module(
{
	name: 'jgrouse.widgets.Expander',
	requires : ['jgrouse.widgets.Container'],
	imports : ['jgrouse.widgets.TemplateParser', 'jgrouse.dom.*'],
	body : function(embed)
	{
		eval(embed);
		
		/**
			Expander widget - a combination of "title" and "content block". Whenever title is clicked, 
			the content block is displayed or hidden. The content block must be assigned to member {@link _block}.
			Expanders could be embedded into each other and share the same MvcDriver and controller, if necessary.
			@class jgrouse.widgets.Expander
			@extends jgrouse.widgets.Container
		*/		
		jgrouse.define('jgrouse.widgets.Expander', 'jgrouse.widgets.Container', function(embed)
		{
			eval(embed);
			jgrouse.widgets.registerWidget('expander', 'jgrouse.widgets.Expander');
			
			
			return {
				/**
					@constructor initialize
					@inheritdesc
				*/
				initialize  : function(options, styles)
				{
					_sup(this, arguments);
					/**
						Widget or DOM element that becomes hidden or displayed
						@variable {DOMElement} _block
					*/
					this._block = null;
				},
				
				/**
					@function render
					@inheritdesc
				*/
				render : function()
				{
					_sup(this, arguments);
					this.setValue(this._value);
				},
				
				/**
					Set indicator if the block should be shown or hidden
					@function setValue
					@param {boolean} value - true if the block should be displayed
				*/
				setValue : function(value)
				{
					this._value = value;
					if (this._domElement)
					{
						Element.addStyle(this._block, 'display', this._value? '' : 'none');
						//TODO: what about widgets in block??
					}
				},
				
				/**
					Sets template parser
					@function setTemplateManager
					@param {jgrouse.widgets.TemplateParser} templateManager
				*/
				setTemplateManager : function(templateManager)
				{
					var templateManager = templateManager.subTemplate();
					this.setTemplate(templateManager);
				}
			}
		});
		
		/**
			Base controller for expanders. Provides handling for triggerNodeToggle event and changes of 
			currentNode property
			@class jgrouse.widgets.ExpanderController
			@extends jgrouse.BaseObject
		*/
		jgrouse.define('jgrouse.widgets.ExpanderController', 'jgrouse.util.BaseController', function(embed)
		{
			eval(embed);
			
			return {
				
				/**
					Callback that is triggered whenever expander's node is being expanded or collapsed
					@function onTriggerNodeToggle
					@param {String} nodeId - identificator of node being toggled
				*/
				onTriggerNodeToggle : function(nodeId)
				{
					this._data[nodeId] = !this._data[nodeId];
					if (this._data[nodeId])
					{
						this.setCurrentNode(nodeId);
					}
				},
				
				/**
				 * Sets which expander's node should be opened. The current node would be 
				 * collapsed
				 * @function setCurrentNode
				 * @param {String} node - node ID
				 */
				setCurrentNode : function(node)
				{
					if (this._data.currentNode)
					{
						this._data[this._data.currentNode] = false;
					}
					this._data.currentNode = node;
					this._data[node] = true;
				}
		}
		});
	}
});